package community.hospital.service.application.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.common.BasePage;
import community.hospital.common.CustomerException;
import community.hospital.convert.CommonConvert;
import community.hospital.convert.HospitalConvert;
import community.hospital.convert.PageConvert;
import community.hospital.entity.Hospital;
import community.hospital.entity.request.HospitalRequest;
import community.hospital.entity.request.ListHospitalRequest;
import community.hospital.entity.response.HospitalOptionResp;
import community.hospital.entity.response.HospitalResponse;
import community.hospital.enums.BusinessEnum;
import community.hospital.service.application.HospitalApplicationService;
import community.hospital.service.domain.HospitalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static community.hospital.enums.BusinessEnum.CREDIT_CODE_REPEAT;
import static community.hospital.enums.BusinessEnum.HOSPITAL_NAME_REPEAT;

/**
 * @Author：ydj
 * @Date：2023/1/27 14:58
 */
@Service
public class HospitalApplicationServiceImpl implements HospitalApplicationService {
    @Resource
    private HospitalService hospitalService;
    @Override
    public Page<Hospital> listHospital(ListHospitalRequest request) {
        return hospitalService.listHospital(request);
    }

    @Override
    public Boolean addHospital(HospitalRequest request) {
        int count = hospitalService.countByCreditCode(request.getCreditCode());
        if (count > 0) {
            throw new CustomerException(CREDIT_CODE_REPEAT);
        }
        Hospital hospital = HospitalConvert.INSTANCE.convertToHospital(request);
        return hospitalService.save(hospital);
    }

    @Override
    public Boolean updateHospital(HospitalRequest request) {
        Hospital hospital = HospitalConvert.INSTANCE.convertToHospital(request);
        return hospitalService.updateById(hospital);
    }

    @Override
    public Boolean removeHospital(List<Long> ids) {
        return  hospitalService.removeBatchByIds(ids);
    }

    @Override
    public HospitalResponse queryById(Long hospitalId) {
        Hospital hospital = hospitalService.getById(hospitalId);
        HospitalResponse hospitalResponse = HospitalConvert.INSTANCE.convertToResp(hospital);
        return hospitalResponse;
    }

    @Override
    public Boolean isHospitalExist(Hospital hospital) {
        int count = StringUtils.isBlank( hospital.getCreditCode() )?0: hospitalService.countByCreditCode(hospital.getCreditCode());
        if (count>0){
            throw new CustomerException(CREDIT_CODE_REPEAT);
        }
        count =StringUtils.isBlank(hospital.getName())?0:hospitalService.countByName(hospital.getName());
        if (count>0){
            throw new CustomerException(HOSPITAL_NAME_REPEAT);
        }
        return true;
    }

    @Override
    public List<HospitalResponse> listOptionHospital() {
        List<Hospital> hospitalList = hospitalService.listOptionHospital();
        return CommonConvert.convert(hospitalList, HospitalConvert.INSTANCE::convertToResp);
    }

    @Override
    public HospitalResponse getHospital(Long id) {
        Hospital hospital = hospitalService.getById(id);
        return HospitalConvert.INSTANCE.convertToResp(hospital);
    }
}
